package com.chum.chamber.controller;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class ExceptionController {
 
    @ExceptionHandler(Exception.class)
    public ModelAndView exception(Exception e) {
         
        ModelAndView mav = new ModelAndView("ErrorDisplayForm");
        mav.addObject("name", e.getClass().getSimpleName());
        
        if (e != null) {
        	String lineBeak = System.getProperty("line.separator");
        	
			StringBuffer errorStackTrace = new StringBuffer();			
			errorStackTrace.append(lineBeak);
			errorStackTrace.append(e.toString());
			errorStackTrace.append(lineBeak);
			
			for(int i=0;e!=null && i < e.getStackTrace().length;i++) {
				errorStackTrace.append(e.getStackTrace()[i]);
				errorStackTrace.append(lineBeak);
			}
			mav.addObject("message", errorStackTrace.toString());
		}
        return mav;
    }
}
